﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MegavaniaX.World;
using MegavaniaX.Messaging.Events;

namespace MegavaniaX.Actors.States
{
    /// <summary>
    /// State for moving, on the ground
    /// </summary>
    public class PlayerStateMove : PlayerStateBase
    {
        private float _initialX;

        private static MegavaniaEventType[] _events = new MegavaniaEventType[]
        {
            MegavaniaEventType.MoveRightReleased,
            MegavaniaEventType.MoveLeftPressed,
            MegavaniaEventType.MoveRightPressed,
            MegavaniaEventType.MoveLeftReleased,
            MegavaniaEventType.JumpPressed,
            MegavaniaEventType.ClimbPressed,
            MegavaniaEventType.ShootPressed,
            MegavaniaEventType.SlidePressed
        };

        public PlayerStateMove(Player _player, float initialX) : base(_player)
        {
            _initialX = initialX;
        }

        public override void OnEnter()
        {
            _player.velocity.X = _initialX;
            _player.CurrentAnimation = _player.animationMove;
            if (_initialX < 0)
                _player.IsFacingLeft = true;
            else
                _player.IsFacingLeft = false;
        }

        public override bool Update(TimeSpan elapsedTime, World.Screen screen)
        {
            if (base.Update(elapsedTime, screen))
                return true;

            if (!_player.IsActorOnSolidGround(screen))
            {
                NextState = new PlayerStateJump(_player, 0.0f);
                return true;
            }

            return false;
        }

        public override bool CanHandleEvent(MegavaniaEventType evtType)
        {
            return _events.Contains(evtType);
        }

        public override bool HandleEvent(IMegavaniaEvent evt)
        {
            if (evt.EventType == MegavaniaEventType.MoveRightPressed && _player.velocity.X <= 0)
            {
                _player.velocity.X = Math.Abs(_initialX);
                _player.IsFacingLeft = false;
            }
            else if (evt.EventType == MegavaniaEventType.MoveLeftPressed && _player.velocity.X >= 0)
            {
                _player.velocity.X = -Math.Abs(_initialX);
                _player.IsFacingLeft = true;
            }
            else if ((evt.EventType == MegavaniaEventType.MoveRightReleased && _player.velocity.X > 0) || (evt.EventType == MegavaniaEventType.MoveLeftReleased && _player.velocity.X < 0))
            {
                _player.velocity.X = 0.0f;
                NextState = new PlayerStateIdle(_player);
                return true;
            }
            else if (evt.EventType == MegavaniaEventType.JumpPressed)
            {
                NextState = new PlayerStateJump(_player, -0.30f);
                return true;
            }
            else if (evt.EventType == MegavaniaEventType.ShootPressed)
            {
                _player.CurrentAnimation = _player.animationShootMoving;
            }
            else if (evt.EventType == MegavaniaEventType.SlidePressed)
            {
                if (_player.velocity.X > 0)
                    NextState = new PlayerStateSlide(_player, Player.InitialXVelo * 1.5f);
                else
                    NextState = new PlayerStateSlide(_player, -Player.InitialXVelo * 1.5f);
                return true;
            }
            else if (evt.EventType == MegavaniaEventType.ClimbPressed)
            {
                var cevt = (ClimbEvent)evt;
                if (HandleClimbEvent(cevt))
                    return true;
            }

            return false;
        }

        public override void StopShootingAnimation()
        {
            _player.CurrentAnimation = _player.animationMove;
        }

        public override void OnExit()
        {
            
        }


    }
}
